package cn.iocoder.yudao.module.crossborder.order.controller.admin.orderreturn;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.order.controller.admin.orderreturn.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.orderreturn.OrderReturnDO;
import cn.iocoder.yudao.module.crossborder.order.service.orderreturn.OrderReturnService;

@Tag(name = "管理后台 - 跨境电商退换货申请")
@RestController
@RequestMapping("/crossborder/order-return")
@Validated
public class OrderReturnController {

    @Resource
    private OrderReturnService orderReturnService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商退换货申请")
    @PreAuthorize("@ss.hasPermission('crossborder:order-return:create')")
    public CommonResult<Long> createOrderReturn(@Valid @RequestBody OrderReturnSaveReqVO createReqVO) {
        return success(orderReturnService.createOrderReturn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商退换货申请")
    @PreAuthorize("@ss.hasPermission('crossborder:order-return:update')")
    public CommonResult<Boolean> updateOrderReturn(@Valid @RequestBody OrderReturnSaveReqVO updateReqVO) {
        orderReturnService.updateOrderReturn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商退换货申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:order-return:delete')")
    public CommonResult<Boolean> deleteOrderReturn(@RequestParam("id") Long id) {
        orderReturnService.deleteOrderReturn(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商退换货申请")
                @PreAuthorize("@ss.hasPermission('crossborder:order-return:delete')")
    public CommonResult<Boolean> deleteOrderReturnList(@RequestParam("ids") List<Long> ids) {
        orderReturnService.deleteOrderReturnListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商退换货申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order-return:query')")
    public CommonResult<OrderReturnRespVO> getOrderReturn(@RequestParam("id") Long id) {
        OrderReturnDO orderReturn = orderReturnService.getOrderReturn(id);
        return success(BeanUtils.toBean(orderReturn, OrderReturnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商退换货申请分页")
    @PreAuthorize("@ss.hasPermission('crossborder:order-return:query')")
    public CommonResult<PageResult<OrderReturnRespVO>> getOrderReturnPage(@Valid OrderReturnPageReqVO pageReqVO) {
        PageResult<OrderReturnDO> pageResult = orderReturnService.getOrderReturnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderReturnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商退换货申请 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:order-return:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderReturnExcel(@Valid OrderReturnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderReturnDO> list = orderReturnService.getOrderReturnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商退换货申请.xls", "数据", OrderReturnRespVO.class,
                        BeanUtils.toBean(list, OrderReturnRespVO.class));
    }

}